package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.mapper.AuthorMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.exception.CustException;
import com.heima.feigns.AdminFeign;
import com.heima.feigns.WemediaFeign;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmNews;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    @Autowired
    private WemediaFeign wemediaFeign;
    @Autowired
    private AdminFeign adminFeign;
    @Autowired
    private AuthorMapper authorMapper;
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @GlobalTransactional(rollbackFor = Exception.class,timeoutMills = 100000)
    @Override
    public void publishArticle(Integer newsId) {
        // 1. 查询并检查自媒体文章  wmNews
        ResponseResult<WmNews> wmNewsResponseResult = wemediaFeign.findWmNewsById(newsId);
        WmNews wmNews = wmNewsResponseResult.getData();
        if (wmNews.getStatus()!=4&&wmNews.getStatus()!=8){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"未审核");
        }
        // 2. 封装 ApArticle
        ApArticle article= new ApArticle();
        BeanUtils.copyProperties(wmNews,article);
        article.setId(wmNews.getArticleId());
        article.setFlag((byte)0); // 普通文章
        article.setLayout(wmNews.getType());// 布局
        ResponseResult<AdChannel> channelResponseResult = adminFeign.findOne(wmNews.getChannelId());
        AdChannel channel = channelResponseResult.getData();
        article.setChannelName(channel.getName());

        //作者信息
        ApAuthor apAuthor = authorMapper.selectOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getWmUserId, wmNews.getUserId()));
        article.setAuthorId(Long.valueOf(apAuthor.getId()));
        article.setAuthorName(apAuthor.getName());

        // 3. 保存或修改 article信息
        if(article.getId() == null){
            article.setCollection(0); // 收藏数
            article.setLikes(0);// 点赞数
            article.setComment(0);// 评论数
            article.setViews(0); // 阅读数
            save(article);
        }else {
            ApArticle apArticle = getById(article.getId());
            updateById(apArticle);
            apArticleConfigMapper.delete(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId,apArticle.getId()));
            apArticleContentMapper.delete(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId,apArticle.getId()));

        }


        // 4. 保存关联配置和内容信息
        saveConfigAndContent(wmNews, article);
        // 5. TODO 文章页面静态化
        // 6. 更新 wmNews状态  改为9  并设置articleId
        wmNews.setStatus(WmNews.Status.PUBLISHED.getCode());
        wmNews.setArticleId(article.getId());
        wemediaFeign.updateWmNews(wmNews);
    }

    private void saveConfigAndContent(WmNews wmNews, ApArticle article) {
        ApArticleConfig apArticleConfig = new ApArticleConfig();
        apArticleConfig.setArticleId(article.getId());
        apArticleConfig.setIsComment(true);
        apArticleConfig.setIsForward(true);
        apArticleConfig.setIsDown(false);
        apArticleConfig.setIsDelete(false);
        apArticleConfigMapper.insert(apArticleConfig);
        // 添加文章详情
        ApArticleContent apArticleContent = new ApArticleContent();
        apArticleContent.setArticleId(article.getId());
        apArticleContent.setContent(wmNews.getContent());
        apArticleContentMapper.insert(apArticleContent);
    }
}